﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Pg.BioMedics.SDR.ServiceAgents.Configuration
{
    /// <summary>
    /// Represents services element in ServiceAgents configuration section
    /// </summary>
    public sealed class ServicesElementCollection : ConfigurationElementCollection
    {
        #region Properties

        /// <summary>
        /// Gets / sets service element at given index
        /// </summary>
        /// <param name="index"></param>
        /// <returns>ServiceElement instance</returns>
        public ServiceElement this[int index]
        {
            get
            {
                return BaseGet(index) as ServiceElement; 
            }

            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);

                BaseAdd(index, value);

            }
        }

        /// <summary>
        /// Gets service element linked to the given service name
        /// </summary>
        /// <param name="serviceName">service name</param>
        /// <returns>Service instance</returns>
        public ServiceElement this[string serviceName]
        {
            get
            {
                return BaseGet(serviceName) as ServiceElement;
            }
        }

        #endregion

        #region Overriden methods

        protected override ConfigurationElement CreateNewElement()
        {
            return new ServiceElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as ServiceElement).ServiceName;
        }

        #endregion
    }
}
